<%layout("/common/_layout.html"){ %>
  <%#ajax userTable:{%>
  <div class="am-cf am-padding">
    <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">业务管理</strong> / <small>选号套餐配置</small></div>
  </div>
  <div class="am-g">
    <div class="am-u-sm-12">
      <form class="am-form" id="list-form" action="${base}/package/list" method="post">
        <input type="hidden" id="keywords" name="keywords" value='${keywords!""}' />
        <table class="am-table am-table-bordered am-table-radius table-main" style="padding:0;">
          <tbody>
            <tr>
              <th class="am-text-middle">号码类型</th>
              <td>
                <div class="am-u-sm-10">
                  <select id="type" data-am-selected="{btnSize: 'sm'}" placeholder="所有">
                    <option value="0" <%if(type! =="0") {%>selected<%}%>>全部</option>
                    <option value="1" <%if(type! == "1") {%>selected<%}%>>普号和靓号</option> 
                    <option value="2" <%if(type! == "2") {%>selected<%}%>>普号</option>
                    <option value="3" <%if(type! == "3") {%>selected<%}%>>靓号</option> 
                    <%if(isShow! == "1"){%>
                        <option value="4" <%if(type! == "4") {%>selected<%}%>>营销活动选号入网</option>
                    <%}%>
                      <option value="5" <%if(type! == "5") {%>selected<%}%>>物联网</option>
                  </select>
                </div>
              </td>
              <th class="am-text-middle">授权类型</th>
                            <td>
                              <div class="am-u-sm-10">
                                <select id="localized" data-am-selected="{btnSize: 'sm'}" placeholder="全部">
                                  <option value="2" <%if(localized! =="2") {%>selected<%}%>>全部</option>
                                  <option value="0" <%if(localized! == "0") {%>selected<%}%>>统一授权</option>
                                  <option value="1" <%if(localized! == "1") {%>selected<%}%>>属地化</option>
                                </select>
                              </div>
                            </td>
            </tr>
            <tr>
              <th class="am-text-middle">资费代码</th>
              <td>
                <div class="am-u-sm-10">
                  <input type="text" id="tariff_code" class="am-form-field am-input-sm"  value="${tariff_code!}"/>
                </div>
              </td>
              <th class="am-text-middle">套餐名称</th>
              <td>
                <div class="am-u-sm-10">
                  <input type="text" id="name" class="am-form-field am-input-sm" value="${name!}"/>
                </div>
              </td>
            </tr>

                        <tr>
                                      <th class="am-text-middle">是否启用</th>
                                      <td>
                                        <div class="am-u-sm-10">
                                          <select id="enabled" data-am-selected="{btnSize: 'sm'}" placeholder="全部">
                                            <option value="2" <%if(enabled! =="2") {%>selected<%}%>>全部</option>
                                            <option value="1" <%if(enabled! == "1") {%>selected<%}%>>启用</option>
                                            <option value="0" <%if(enabled! == "0") {%>selected<%}%>>禁用</option>
                                          </select>
                                        </div>
                                      </td>
                                      <th class="am-text-middle"></th>
                                                    <td>
                                                      <div class="am-u-sm-10">
                                                      </div>
                                                    </td>
                      </tr>

            <tr>
                <td colspan="4">
                     <span class="am-text-nowrap am-text-danger am-l">*省账号请勿编辑地市套餐（特别是统一授权/属地化）！否则地市配置将丢失，无法修复！</span>
                     <br>
                     <span class="am-text-nowrap am-text-warning am-l">*删除套餐后，将完全删除资费套餐，同时删除所有关于其权限配置数据（前端不再可见，数据无法恢复）,请谨慎删除！</span>
                     <br>
                     <span class="am-text-nowrap am-text-primary am-l">*地市若创建套餐，统一授权/属地化无效，创建后本地市后台账号可见，配置后APP可见。</span>
                     <br>
                </td>
                <td colspan="4">
                  <div align='right'>
                    <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary" onClick="submit_btn('search');">搜索</button>
                    <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary" onClick="submit_btn('export');">导出</button>
                  </div>
                </td>
            </tr>

          </tbody>
        </table>
      </form>
    </div>
    <div class="am-u-sm-12 am-u-md-12" style="padding:0 1.6rem 1.6rem 1rem;margin:0;">
      <div class="am-btn-toolbar" style="padding-left:.5rem;">
        <div class="am-btn-group am-btn-group-xs">
          <%if(so.hasPermission("PACKAGE_ADD")) {%>
            <button type="button" class="am-btn am-btn-default" onclick="location.href='${base}/package/add'"><span class="am-icon-plus"></span> 新增</button>
          <%}%>
          <%if(so.hasPermission("PACKAGE_DEL")) {%>
            <button type="button" id="deleteButton" disabled="disabled" class="am-btn am-btn-default"  onclick="deleteAll('${base}/package/delete')"><span class="am-icon-trash-o"></span> 删除</button>
          <%}%>
          <%if(so.hasPermission("PACKAGE_ENABLE")) {%>
            <button type="button" id="enableButton" class="am-btn am-btn-default" onclick="enableAll('${base}/package/enable')" disabled>
                <span class="am-icon-toggle-on"></span>
                启用
            </button>
          <%}%>
          <%if(so.hasPermission("PACKAGE_DISABLE")) {%>
            <button type="button" id="disableButton" class="am-btn am-btn-default" onclick="disableAll('${base}/package/disable')" disabled>
                <span class="am-icon-toggle-off"></span>
                禁用
            </button>
          <%}%>
        </div>
      </div>
    </div>
  </div>
  <div class="am-g">
    <div class="am-u-sm-12">
      <table class="am-table am-table-striped am-table-hover table-main">
        <thead>
          <tr class="am-text-nowrap">
            <th class="table-check"><input type="checkbox"  id="allCheck"/></th>
            <th class="table-title">套餐名称</th>
            <th class="table-title">号码类型</th>            
            <th class="table-title">资费代码</th>
            <th class="table-title">资费(元/月)</th>
            <th class="table-title">预存费用(元)</th>
            <th class="table-title">品牌编码</th>
            <th class="table-title">特点描述</th>
            <th class="table-title">创建时间</th>
            <th class="table-title">创建人</th>
            <th class="table-title">创建人的机构</th>
            <th class="table-title">授权属性</th>
            <th class="table-title">属地化城市</th>
            <th class="table-title">是否启用</th>
              <th class="table-title">排序</th>
            <%if(so.hasPermission("PACKAGE_EDIT")) {%>   
            <th class="table-set">操作</th>
            <%}%>
          </tr>
        </thead>
        <tbody>
          <%for(list in pager.list!){%>
            <tr>
              <td><input type="checkbox" name="ids" value="${list.id}"></td>
              <td>
               <%if(so.hasPermission("PACKAGE_EDIT")) {%>
              <a href="${base}/package/edit?edit=1&id=${list.id}">${list.name}</a>
               <%}else{%>
               ${list.name}
              <%}%>
              </td>
              <td>
                    <%if (list.type==0){%>
                                                                       普号
                    <%}else if (list.type==1){%>
                                                                      靓号
                  <%}else if (list.type==3){%>
                                                                        营销活动选号入网
                  <%}else if (list.type==5){%>
                  物联网
                  <%}else{%>
                                                                      普号和靓号
                    <%}%>
                  </td>
              <td>${list.tariff_code}</td>
              <td>${list.monthly_charge}</td>
              <td>${list.predeposited}</td>
              <td>${list.brand_id}</td>
              <td>${list.summary}</td>
              <td>${list.created_time,dateFormat="yyyy/MM/dd HH:mm:ss"}</td>
              <td>${list.creater_name}</td>
              <td>${list.creater_org}</td>
              <td id='auth_attr'><%if(list.localized==0) {%>
                                统一授权
                               <%}%>
                               <%else{%>
                                属地化
                               <%}%>
              </td>
              <td>${list.cities}</td>
              <td id='broadband_enabled'><%if(list.enabled==1) {%>
                  是                                                                  
                 <%}%>
                 <%else{%>
                  否
                 <%}%>                  
              </td>
                <td>${list.sort}</td>
                <%if(so.hasPermission("PACKAGE_EDIT")) {%>
                <td id='operated'>
                  <div id ="edit-div" class="am-btn-toolbar <%if(list.enabled == 1) {%>am-hide<%}%>">
                    <div class="am-btn-group am-btn-group-xs">   
                      <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="location.href='${base}/package/edit?edit=1&id=${list.id}'"><span class="am-icon-pencil-square-o"></span>修改</button>
                    </div>
                  </div>                
                </td>
              <%}%>
            </tr>
           <%}%> 
        </tbody>
     </table>
      <div class="am-cf">
         <!-- 分页 -->
         <%if (pager.list.~size > 0){%>
             <div class="am-fr">
                 <% include("/admin/common_pager.tpl"){}%>
             </div>
         <%}else{%>
             <div class="am-kai">
               <h3>没有找到任何记录!</h3>
             </div>
         <%}%>
      </div>
    </div>
  </div>
  <script type="text/javascript">
  var keywordsObj = {};
   var submit_btn = function(businessType){
         if($("#type").val() && $("#type").val() != "0")
                     keywordsObj.type = $("#type").val();
                 if($("#tariff_code").val())
                     keywordsObj.tariff_code = $("#tariff_code").val();
                 if($("#name").val())
                     keywordsObj.name = $("#name").val();
                 if($("#localized").val()!=2){
                  keywordsObj.localized = $("#localized").val();
                 }
                 if($("#enabled").val()!=2){
                  keywordsObj.enabled = $("#enabled").val();
                 }

               var keywords = "";
               if(!$.isEmptyObject(keywordsObj)){
                 keywords = JSON.stringify(keywordsObj);
               }
               console.log("keywords = "+ keywords);

               $("#keywords").val(keywords);



   	if(businessType == "search"){
   		window.location.href = "${base}/package/list?keywords="+keywords;
   	}else if(businessType == "export"){
   		window.location.href = "${base}/package/export?keywords="+keywords;
   	}

   }
    var urlBase = "${base}";
    var url;
    $(document).ready(function() {
      var $allCheck = $("#allCheck");// 全选复选框
      var $idsCheck = $('[name=ids]:checkbox');// ID复选框
      var $enableButton = $("#enableButton");// 删除按钮
      var $disableButton = $("#disableButton");// 查询按钮
      $allCheck.checkAll();
      // 全选
      $allCheck.click(function() {
          $allCheck.checkAll();
          var isChecked = $allCheck.is(":checked");
          if (isChecked == false) {
              $enableButton.prop("disabled", true);
              $disableButton.prop("disabled", true);
          } else {
              $enableButton.prop("disabled", false);
              $disableButton.prop("disabled", false);
          }
      });

      // 无复选框被选中时,删除按钮不可用
      $idsCheck.click(function() {
          var $idsChecked = $("[name='ids']:checked");
          if ($idsChecked.size() > 0) {
              $enableButton.prop("disabled", false);
              $disableButton.prop("disabled", false);
          } else {
              $enableButton.prop("disabled", true);
              $disableButton.prop("disabled", true);
          }
      });   
    });   

    function enableAll(url){
      batchProcess($("#enableButton"), url, '启用','是');
    }

    function disableAll(url){
      batchProcess($("#disableButton"), url, '禁用','否');
    }  

    function batchProcess(btnObj, url, actinoName, value) {
      var $idsCheckedCheck = $("[name='ids']:checked");
      var $deleteButton = btnObj;// 删除按钮
      var ids = $idsCheckedCheck.serialize();
      if (confirm('您确定要'+actinoName+'吗？') == true) {       
          $.ajax({
            url : url,
            data : ids,
            dataType : "json",
            async : false,
            beforeSend : function(data) {
                $deleteButton.prop("disabled", true)
            },
            success : function(data) {
              $deleteButton.prop("disabled", false)
              if (data.status == "success") {
                $idsCheckedCheck.parent().parent().find('#broadband_enabled').html(value);
                if(actinoName == '禁用'){                        
                  $idsCheckedCheck.parent().parent().find('#operated').find('#edit-div').removeClass('am-hide');                       
                }else if(actinoName =='启用'){
                  $idsCheckedCheck.parent().parent().find('#operated').find('#edit-div').addClass('am-hide');
                }
              }
              sendMessage({
                title : actinoName,
                text : data.message,
                type : "success",
                timer : 2000,
                showConfirmButton : false
              });                
            }
          });
        }
      }
  </script>
  <%}%>
<%}%>



